<?php defined('BASEPATH') OR exit('No direct script access allowed');
/**
 * Description: 计量单位
 *
 * @author      Eden
 * @datetime    2016/11/27 16:00
 * @copyright   Beijing CmsTop Technology Co.,Ltd.
 */

class Unit extends ADM_Controller
{
    /**
     * 初始化
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * 获取列表
     */
    public function getLists()
    {
        $this->load->model('admin/goods/Unit_Model', 'unit');
        $res = $this->unit->getLists();
        $send['state'] = true;
        $send['data'] = empty($res) ? [] : $res;
        $this->output->send($send);
    }

    /**
     * 删除单位
     */
    public function removeUnit()
    {
        $send['state'] = false;
        do {

            $unitId = $this->input->get('unitId');
            if (empty($unitId)) {
                $send['error'] = '参数错误';
                break;
            }

            $this->load->model('admin/goods/Unit_Model', 'unit');
            if ($res = $this->unit->removeUnit($unitId)) {
                $send['state'] = true;
            } else {
                $error = $this->unit->getError();
                $send['error'] = !empty($error) ? $error : '添加失败';
            }
        } while(false);
        $this->output->send($send);
    }

    /**
     * 添加单位
     */
    public function addUnit()
    {
        $send['state'] = false;
        do {

            $name = $this->input->post('name');
            if (empty($name)) {
                $send['error'] = '参数错误';
                break;
            }
            
            $this->load->model('admin/goods/Unit_Model', 'unit');
            if ($res = $this->unit->addUnit($name)) {
                $send['state'] = true;
                $send['data'] = $res;
                break;
            }
            $send['error'] = '添加失败';
        } while(false);
        $this->output->send($send);
    }

}
